package com.tuananh.radnom.persist.imp.xml;

import java.util.ArrayList;

import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;
import com.tuananh.radnom.model.netservice.NetService;
import com.tuananh.radnom.model.netservice.NetServiceConfiguration;
import com.tuananh.radnom.persist.INetServiceDAO;

public class NetServiceXmlDAO extends XmlDAO implements INetServiceDAO {
	NetServiceConfiguration netServiceConfig;

	public NetServiceXmlDAO() {
		super(NetServiceConfiguration.path);
		configuration = XmlConfigHolder.netServiceConfig;
		try {
			savedConfiguration = (new DeepCopyUtil()).deepCopy(configuration);
		} catch (DeepCopyException e) {
			// TODO Auto-generated catch block
			savedConfiguration = new NetServiceConfiguration();
			e.printStackTrace();
		}
		netServiceConfig = (NetServiceConfiguration) configuration;
		
	}

	public boolean add(NetService netService) {
		if (getByName(netService.getName()) == null)
			netServiceConfig.getService().add(netService);
		return true;
	}

	public ArrayList<NetService> getAll() {
		// TODO Auto-generated method stub
		return netServiceConfig.getService();
	}

	public boolean update(NetService netService) {
		// TODO Auto-generated method stub
		for (int i = 0; i < netServiceConfig.getService().size(); i++) {
			NetService result = netServiceConfig.getService().get(i);
			if (result.getName().equals(netService.getName())) {
				netServiceConfig.getService().set(i, netService);
				return true;
			}
		}
		return false;
	}

	public boolean remove(String name) {
		// TODO Auto-generated method stub
		NetService remove = getByName(name);
		if (remove != null) {
			netServiceConfig.getService().remove(remove);
			return true;
		}
		return false;
	}

	public NetService getByName(String name) {
		// TODO Auto-generated method stub
		for (int i = 0; i < netServiceConfig.getService().size(); i++) {
			NetService result = netServiceConfig.getService().get(i);
			if (result.getName().equals(name))
				return result;
		}
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "NetServiceEvent";
	}

}
